//
//  BNRItem.m
//  Quiz
//
//  Created by YaoJacob on 15/11/1.
//  Copyright © 2015年 Roger. All rights reserved.
//

#import "BNRItem.h"

@implementation BNRItem

//class.static method
+(instancetype)randomItem
{
    NSArray *randomAdjectiveList = @[@"Fluffy", @"Rustry", @"pteey"];
    NSArray *randomNounList = @[@"Bear", @"Big", @"Apple"];
    
    NSInteger adjectIndex = arc4random() % randomAdjectiveList.count;
    NSInteger nounIndex = arc4random() % randomNounList.count;
    
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            randomAdjectiveList[adjectIndex],
                            randomNounList[nounIndex]];
    
    int randomValue = arc4random() % 100;
    NSString * randomSeriaNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0'+arc4random() %10,
                                    'A'+arc4random() % 26,
                                    '0'+arc4random() % 10,
                                    'a'+arc4random() % 26,
                                    '0' + arc4random() % 10];
    
    BNRItem *item = [[BNRItem alloc] initWithItemName:randomName valueInDollars:randomValue serialNumber:randomSeriaNumber];
    return item;
}

-(instancetype)initWithItemName:(NSString*)name valueInDollars:(int)value serialNumber:(NSString*) sNumber
{
    self = [super init];
    if (self) {
        self.itemName = name;
        _valueInDollars = value;
        _serialNumber = sNumber;
        _dateCreated = [[NSDate alloc]init];
        
        NSUUID *uuid = [[NSUUID alloc]init];
        NSString *key = [uuid UUIDString];
        _itemKey = key;
    }
    
    
    return  self;
}

-(instancetype)initWithItemName:(NSString*)name
{
    return [self initWithItemName:name valueInDollars:100 serialNumber:@"a-b-c"];
}

-(instancetype)initWithItemName:(NSString*)name serialNumber:(NSString*) sNumber
{
    return [self initWithItemName:name valueInDollars:999 serialNumber:sNumber];
}

//-(void)setItemName:(NSString *)str
//{
//    _itemName = str;
//}
//
//-(NSString*)itemName
//{
//    return _itemName;
//}
//
//-(void)setSerialNumber:(NSString*)str
//{
//    _serialNumber = str;
//}
//
//-(NSString*)serialNumber
//{
//    return _serialNumber;
//}
//
//-(void)setValueInDollars:(int)v
//{
//    _valueInDollars = v;
//}
//-(int)valueInDollars
//{
//    return _valueInDollars;
//}
//
//-(NSDate*)dateCreated
//{
//    return _dateCreated;
//}

-(void)dealloc
{
    NSLog(@"destory: %@", self);
}

-(NSString*)description
{
    NSString *ret = [[NSString alloc] initWithFormat:@"%@, %@, %d, %@" ,
                     self.itemName,
                     self.serialNumber,
                     self.valueInDollars,
                     self.dateCreated];
    
    return ret;
}

//-(void)setContainedItem:(BNRItem*)str
//{
//    self.containedItem = str;
//    
//    str.container = self;
//}
//
//-(BNRItem*)containedItem
//{
//    return _containedItem;
//}
//
//-(void)setContainer:(BNRItem*)str
//{
//    _container = str;
//}
//
//-(BNRItem*)container
//{
//    return _container;
//}

#pragma Archiving
//save
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.itemName forKey:@"itemName"];
    [aCoder encodeObject:self.itemKey forKey:@"itemKey"];
    [aCoder encodeObject:self.dateCreated forKey:@"dateCreated"];
    [aCoder encodeObject:self.serialNumber forKey:@"serialNumber"];
    [aCoder encodeInt:self.valueInDollars forKey:@"valueInDollars"];
}

//load
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        _itemName = [aDecoder decodeObjectForKey:@"itemName"];
        _itemKey = [aDecoder decodeObjectForKey:@"itemKey"];
        _dateCreated = [aDecoder decodeObjectForKey:@"dateCreated"];
        _serialNumber = [aDecoder decodeObjectForKey:@"valueInDollars"];
        _valueInDollars = [aDecoder decodeIntForKey:@"valueInDollars"];
    }
    return self;
}
@end
